{extend name='common/_container'}
{block name="content"}
<div class="ibox float-e-margins">
<input type="hidden" name='content_id' id='content_id' value="{$info.content_id}" />
	<div class="ibox-content">
		<div class="form-horizontal" id="CodeInfoForm">
			<div class="row" style="margin-top:-20px;">
				<div class="layui-tab layui-tab-brief" lay-filter="test">
					<ul class="layui-tab-title">
						<li class="layui-this">基本信息</li>
						<li>拓展信息</li>
					</ul>
					<div class="layui-tab-content" style="margin-top:10px;">
						<div class="layui-tab-item layui-show">
							<div class="col-sm-12">
							<!-- form start -->
					<div class="form-group">
						<label class="col-sm-1 control-label">文章标题：</label>
						<div class="col-sm-9">
							<input type="text" id="title" value="{$info.title}" name="title" class="form-control" placeholder="请输入文章标题">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">所属分类：</label>
						<div class="col-sm-9">
							<select lay-ignore name="class_id" class="form-control" id="class_id">
								<option value="">请选择</option>
								{sql query="select class_id,class_name,pid from cd_catagory"}
									<option value="{$sql.class_id}" {if condition="$info.class_id eq $sql.class_id"}selected{/if}>{$sql.class_name}</option>
								{/sql}
							</select>
						</div>
					</div>
					<div class="form-group layui-form">
						<label class="col-sm-1 control-label">缩略图：</label>
						<div class="col-sm-6" style="padding-right:0">
							<input type="text" id="pic" value="{$info.pic}" name="pic" {if condition="config('my.img_show_status') eq true"}onmousemove="showBigPic(this.value)" onmouseout="closeimg()"{/if} class="form-control" placeholder="请输入缩略图">
							<span class="help-block m-b-none pic_process"></span>
						</div>
						<div class="col-sm-1" style="padding-left:0; padding-right:0">
							<span id="pic_upload"></span>
							
						</div>
						<div class="col-sm-4" style="position:relative; right:30px; top:5px;">
							<input name="checkpic" id="checkpic" value="" lay-skin="primary" lay-filter="checkpic" type="checkbox" title="提取内容第一张图作为缩略图">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">文章详情：</label>
						<div class="col-sm-9">
							<script id="detail" type="text/plain" name="detail" style="width:100%;height:300px;">{$info.detail}</script>
							<script type="text/javascript">
								var ue = UE.getEditor('detail',{serverUrl : '{:url("admin/Upload/uploadUeditor")}'});
								scaleEnabled:true
							</script>
						</div>
					</div>
					<div id="extend"></div>
							<!-- form end -->
							</div>
						</div>
						<div class="layui-tab-item">
							<div class="col-sm-12">
							<!-- form start -->
					<div class="form-group layui-form">
						<label class="col-sm-1 control-label">文章状态：</label>
						<div class="col-sm-9">
							<?php if(!isset($info['status'])){ $info['status'] = 1; }; ?>
							<input name="status" value="1" type="radio" {if condition="$info.status eq '1'"}checked{/if} title="正常">
							<input name="status" value="0" type="radio" {if condition="$info.status eq '0'"}checked{/if} title="下架">
						</div>
					</div>
					<div class="form-group layui-form">
						<label class="col-sm-1 control-label">推荐位：</label>
						<div class="col-sm-9">
								{sql query="select position_id,title from cd_position"}
									<input name="position" value="{$sql.position_id}" type="checkbox" {if in_array($sql.position_id,explode(',',$info['position']))}checked{/if} title="{$sql.title}">
								{/sql}
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">跳转：</label>
						<div class="col-sm-9">
							<input type="text" id="jumpurl" value="{$info.jumpurl}" name="jumpurl" class="form-control" placeholder="请输入跳转">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">发布时间：</label>
						<div class="col-sm-9">
							<input type="text" value="{if condition="$info.create_time neq ''"}{$info.create_time|date="Y-m-d H:i:s"}{/if}" name="create_time"  placeholder="请输入发布时间" class="form-control layer-date"  id="create_time">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">关键词：</label>
						<div class="col-sm-9">
							<input type="text" id="keyword" value="{$info.keyword}" name="keyword" data-role="tagsinput" class="form-control" placeholder="请输入关键词">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">描述：</label>
						<div class="col-sm-9">
							<textarea id="description" name="description"  class="form-control" placeholder="请输入描述">{$info.description}</textarea>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">浏览量：</label>
						<div class="col-sm-9">
							<input type="text" id="views" value="{$info.views}" name="views" class="form-control" placeholder="请输入浏览量">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">排序：</label>
						<div class="col-sm-9">
							<input type="text" id="sortid" value="{$info.sortid}" name="sortid" class="form-control" placeholder="请输入排序">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-1 control-label">来源：</label>
						<div class="col-sm-9">
							<input type="text" id="author" value="{$info.author}" name="author" class="form-control" placeholder="请输入来源">
						</div>
					</div>
							<!-- form end -->
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="row btn-group-m-t">
				<div class="col-sm-9 col-sm-offset-1">
					<button type="button" class="btn btn-primary" onclick="{if condition="$info.content_id neq ''"}CodeInfoDlg.update(){else/}CodeInfoDlg.add(){/if}" id="ensure">
						<i class="fa fa-check"></i>&nbsp;确认提交
					</button>
					<button type="button" class="btn btn-danger" onclick="CodeInfoDlg.close()" id="cancel">
						<i class="fa fa-eraser"></i>&nbsp;取消
					</button>
				</div>
			</div>
		</div>
	</div>
</div>
<script src="__PUBLIC__/static/js/upload.js" charset="utf-8"></script>
<script src="__PUBLIC__/static/js/plugins/layui/layui.js?t=1498856285724" charset="utf-8"></script>
<link  href='__PUBLIC__/static/js/plugins/tagsinput/tagsinput.css' rel='stylesheet'>
<script type='text/javascript' src='__PUBLIC__/static/js/plugins/tagsinput/tagsinput.min.js'></script>
<link href='__PUBLIC__/static/js/plugins/chosen/chosen.min.css' rel='stylesheet'/>
<script src='__PUBLIC__/static/js/plugins/chosen/chosen.jquery.js'></script>
<script src='__PUBLIC__/static/js/plugins/paixu/jquery-migrate-1.1.1.js'></script>
<script src='__PUBLIC__/static/js/plugins/paixu/jquery.dragsort-0.5.1.min.js'></script>
<script>
laydate.render({elem: '#create_time',type: 'datetime'});
layui.use(['form'], function () {
	form = layui.form;
	form.on('checkbox(checkpic)',function(data){
		if(data.elem.checked){
			var ajax = new $ax(Feng.ctxPath + "/Cms.Content/getThumbPic", function (data) {
				if(data.status == '00'){
					$("#pic").val(data.imgurl);
				}
			});
			var detail = UE.getEditor('detail').getContent();
			ajax.set('detail', detail);
			ajax.start();
		}
	});
	
});
layui.use('element', function(){
	var element = layui.element;
	element.on('tab(test)', function(elem){});
});
uploader('pic_upload','pic','image',false,'','{:url("admin/Upload/uploadImages")}');


$(function(){
	$('#class_id').change(function() {
		//此段主要是为了加载当前class_id  否则可以不用写
		var ajax = new $ax(Feng.ctxPath + "/Cms.Content/getExtends", function (data) {
			uploader('pic_upload','pic','image',false,'','{:url("admin/Upload/uploadImages")}?upload_config_id=' + data.upload_config_id); 
			if(data.status == '00'){
				$("#extend").html(data.data);
				var fieldList = data.fieldList;
				for(var p in fieldList){
					//单图上传
					if(fieldList[p]['type'] == 8){
						uploader(fieldList[p]['field']+'_upload',fieldList[p]['field'],'image',false,'','{:url("admin/Upload/uploadImages")}?upload_config_id=' + data.upload_config_id);
					}
					//多图上传
					if(fieldList[p]['type'] == 9){
						var images = $("#"+fieldList[p]['field']+'_images').val();
						var default_value;
						if(images !== ''){
							var images_value;
							if(images.indexOf("|") == -1){
								images_value = '["'+images+'"]';
							}else{
								images = (images.substring(images.length - 1) == '|') ? images.substring(0, images.length - 1) : images;
								images_value = JSON.stringify(images.split("|").reverse());
							}
							default_value = images_value;				
						}else{
							default_value = '';
						}
						
						uploader(fieldList[p]['field']+'_upload',fieldList[p]['field'],'image',true,default_value,'{:url("admin/Upload/uploadImages")}?upload_config_id=' + data.upload_config_id);
						setUploadButton(fieldList[p]['field']+'_upload');
						$(".filelist").dragsort({dragSelector: "img",dragBetween: true ,dragEnd:function(){}});
					}
					//文件上传
					if(fieldList[p]['type'] == 10){				
						uploader(fieldList[p]['field']+'_upload',fieldList[p]['field'],'file',false,'','{:url("admin/Upload/uploadImages")}');
					}
					
					if(fieldList[p]['type'] == 27){	
						$('.chosen').chosen({});
					}
					
					if(fieldList[p]['type'] == 28){	
						$("input[data-role=tagsinput]").tagsinput();
					}
					
					layui.use(['form'], function () {
						form = layui.form;
						form.render();
					});				
				}
			}else{
				$("#extend").html('');
			}
			
		});
		ajax.set('class_id', $(this).find('option:selected').val());
		ajax.set('content_id', $("#content_id").val());
		ajax.start();
	})
	{if condition="$info.content_id neq ''"}
	$('#class_id').change();
	{/if}
})

var CodeInfoDlg = {
	CodeInfoData: {},
	validateFields: {
		class_id: {
			validators: {
				notEmpty: {
					message: '所属栏目不能为空'
	 			},
	 		}
	 	},
	 }
}

CodeInfoDlg.collectData = function () {
	this.set('content_id').set('title').set('class_name').set('class_id').set('pic').set('content').set('jumpurl').set('create_time').set('keyword').set('description').set('views').set('sortid').set('author');
};

CodeInfoDlg.add = function () {
	 this.clearData();
	 this.collectData();
	 if (!this.validate()) {
	 	return;
	 }
	 var detail = UE.getEditor('detail').getContent();
	 var status = $("input[name = 'status']:checked").val();
	 var position = '';
	 $('input[name="position"]:checked').each(function(){ 
	 	position += ',' + $(this).val(); 
	 }); 
	  position = position.substr(1); 
	 var tip = '添加';
	 var ajax = new $ax(Feng.ctxPath + "/Cms.Content/add", function (data) {
	 	if ('00' === data.status) {
	 		Feng.success(tip + "成功" );
	 		window.parent.CodeGoods.table.refresh();
	 		CodeInfoDlg.close();
	 	} else {
	 		Feng.error(tip + "失败！" + data.msg + "！");
		 }
	 }, function (data) {
	 	Feng.error("操作失败!" + data.responseJSON.message + "!");
	 });
	 
	 ajax.set('status',status);
	 ajax.set('position',position);
	 ajax.set('detail',detail);
	 ajax.set(this.CodeInfoData);
	 
	 var entend = new $ax(Feng.ctxPath + "/Cms.Content/getExtends", function (data) {
		var fieldList = data.fieldList;
		for(var p in fieldList){
			var type = fieldList[p]['type'];
			
			if(type == 3){
				ajax.set(fieldList[p]['field'],$("input[name = '"+fieldList[p]['field']+"']:checked").val());
			}else if(type == 4){	
				var checkData = '';
				$('input[name="'+fieldList[p]['field']+'"]:checked').each(function(){ 
					checkData += ',' + $(this).val(); 
				});
				checkData = checkData.substr(1);
				
				ajax.set(fieldList[p]['field'],checkData);
			}else if(type == 9){	
				var images = '';
				$("."+fieldList[p]['field']+" img").each(function() {
					images += '|'+$(this).attr('src');
				});
				images = images.substr(1);
				ajax.set(fieldList[p]['field'],images);
			}else if(type == 16){
				ajax.set(fieldList[p]['field'],UE.getEditor(''+fieldList[p]['field']+'').getContent());
			}else if(type == 17){
				var areaaddress = fieldList[p]['field'].split('|');
				for (var i = 0; i < areaaddress.length; i++){
					ajax.set(areaaddress[i],$("#"+areaaddress[i]).val());
				}
			}else{
				ajax.set(fieldList[p]['field'],$("#"+fieldList[p]['field']).val());
			}
		}
		
	 });
	entend.set('class_id', $("#class_id option:selected").val());
	entend.start();
	 
	ajax.start();
};

CodeInfoDlg.update = function () {
	 this.clearData();
	 this.collectData();
	 if (!this.validate()) {
	 	return;
	 }
	 var detail = UE.getEditor('detail').getContent();
	 var status = $("input[name = 'status']:checked").val();
	 var position = '';
	 $('input[name="position"]:checked').each(function(){ 
	 	position += ',' + $(this).val(); 
	 }); 
	  position = position.substr(1); 
	 var tip = '修改';
	 var ajax = new $ax(Feng.ctxPath + "/Cms.Content/update", function (data) {
	 	if ('00' === data.status) {
	 		Feng.success(tip + "成功" );
	 		window.parent.CodeGoods.table.refresh();
	 		CodeInfoDlg.close();
	 	} else {
	 		Feng.error(tip + "失败！" + data.msg + "！");
		 }
	 }, function (data) {
	 	Feng.error("操作失败!" + data.responseJSON.message + "!");
	 });
	 
	 
	 ajax.set('status',status);
	 ajax.set('position',position);
	 ajax.set('detail',detail);
	 ajax.set(this.CodeInfoData);
	 
	 var entend = new $ax(Feng.ctxPath + "/Cms.Content/getExtends", function (data) {
		var fieldList = data.fieldList;
		for(var p in fieldList){
			var type = fieldList[p]['type'];
			
			if(type == 3){
				ajax.set(fieldList[p]['field'],$("input[name = '"+fieldList[p]['field']+"']:checked").val());
			}else if(type == 4){	
				var checkData = '';
				$('input[name="'+fieldList[p]['field']+'"]:checked').each(function(){ 
					checkData += ',' + $(this).val(); 
				});
				checkData = checkData.substr(1);
				
				ajax.set(fieldList[p]['field'],checkData);
			}else if(type == 9){	
				var images = '';
				$("."+fieldList[p]['field']+" img").each(function() {
					images += '|'+$(this).attr('src');
				});
				images = images.substr(1);
				ajax.set(fieldList[p]['field'],images);
			}else if(type == 16){
				ajax.set(fieldList[p]['field'],UE.getEditor(''+fieldList[p]['field']+'').getContent());
			}else if(type == 17){
				var areaaddress = fieldList[p]['field'].split('|');
				for (var i = 0; i < areaaddress.length; i++){
					ajax.set(areaaddress[i],$("#"+areaaddress[i]).val());
				}
			}else{
				ajax.set(fieldList[p]['field'],$("#"+fieldList[p]['field']).val());
			}
		}
		
	 });
	 
	entend.set('class_id', $("#class_id option:selected").val());
	entend.start();
	 
	ajax.start();
};


</script>
<script src="__PUBLIC__/static/js/base.js" charset="utf-8"></script>

{/block}
